package lib

import "time"

type STime struct {
	NowAt   int64 // 当前时间戳
	TMStart int64 // 当前时间戳上个月的1号的0晨时间戳
	MStart  int64 // 当前时间戳所在月的1号的0晨时间戳
	Day     int64 // 几号
	MEnd    int64 // 当前时间戳所在月末
	WStart  int64 // 当前时间戳所在周周一的0晨时间戳
	Week    int64 // 星期几
	WEnd    int64 // 当前时间戳所在周下周周一的0晨时间戳
	ZUnix   int64 // 当前时间戳的凌晨
}

func (this *STime) Zeller(at time.Time) int {
	var y, m, c int
	if at.Month() >= 3 {
		m = int(at.Month())
		y = at.Year() % 100
		c = at.Year() / 100
	} else {
		m = int(at.Month()) + 12
		y = (at.Year() - 1) % 100
		c = (at.Year() - 1) / 100
	}
	week := y + (y / 4) + (c / 4) - 2*c + ((26 * (m + 1)) / 10) + at.Day() - 1
	if week < 0 {
		week = 7 - (-week)%7
	} else {
		week = week % 7
	}
	if week == 0 {
		return 7
	}
	return week
}

func (this *STime) Weeks(unix time.Time) (int64, int64) {
	this.Week = int64(this.Zeller(unix))
	z_now := unix.Unix() - (unix.Unix()+28800)%86400
	this.WStart = z_now - int64((this.Week-1)*86400)
	this.WEnd = this.WStart + 604800
	return this.WStart, this.WEnd
}

func (this *STime) Month(unix time.Time) (int64, int64) {
	this.Day = int64(unix.Day())
	this.MStart = time.Date(unix.Year(), unix.Month(), 1, 0, 0, 0, 0, time.Local).Unix()
	if unix.Month() != 12 {
		this.MEnd = time.Date(unix.Year(), unix.Month()+1, 1, 0, 0, 0, 0, time.Local).Unix()
	} else {
		this.MEnd = time.Date(unix.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).Unix()
	}
	if unix.Month() != 1 {
		this.TMStart = time.Date(unix.Year(), unix.Month()-1, 1, 0, 0, 0, 0, time.Local).Unix()
	} else {
		this.TMStart = time.Date(unix.Year()-1, 12, 1, 0, 0, 0, 0, time.Local).Unix()
	}
	return this.MStart, this.MEnd
}

func (this *STime) Zero(unix time.Time) int64 {
	this.ZUnix = unix.Unix() - (unix.Unix()+28800)%86400
	return this.ZUnix
}

func (this *STime) All(at int64) {
	unix := time.Unix(at, 0)
	this.Weeks(unix)
	this.Month(unix)
	this.Zero(unix)
}

/*
func main(){
	var val STime
	val.All( time.Now().Unix() )
	fmt.Printf( "%+v\n", val )
}
*/
